<?php 
class Reservation extends CI_Controller
{
	public function status()
	{
		//load model
		$this->load->model('Get_DB_Data_model');
		
		//get parameter
		$key = $this->uri->segment(3);
		
		//find data in DB
		$check_status = $this->Get_DB_Data_model->get_reservation_data_from_key($key);
		
		if($check_status != false)
		{
			$data['status'] = $check_status;
		}
		
		//get name from member id
		foreach ($check_status as $row) {
			$data['member'] = $this->Get_DB_Data_model->get_name_from_id($row->memid);
		}
		
		//load view
		$this->load->view('template/header');
		$this->load->view('reservestatus', $data);
		$this->load->view('template/footer');
	}
	
	//query reservation detail
	public function showdetail()
	{
		if($day = $this->input->post('day'))
		{
			//get room session
			$room = $this->session->userdata('room');
			
			//set date selected session
			$this->session->set_userdata('date', $day);
				
			//load model
			$this->load->model('Get_DB_Data_model');
			$result = $this->Get_DB_Data_model->get_reservation_detail($room, $day);
				
			if($result != null){
				$content = "<table border='1'><tr><th>ชื่อผู้จอง</th><th>เวลาที่เริ่ม</th><th>เวลาสิ้นสุด</th><th>สถานะ</th></tr>";
				
				foreach ($result as $row) {
					$name = $this->Get_DB_Data_model->get_name_surname_string_from_id($row->memid);
					$content = $content ."<tr><td>" .$name ."</td><td>" .$row->usetime_start ."</td><td>" .$row->usetime_end ."</td><td>" .$row->resstatus ."</td></tr>"; 
				}
				$content = $content ."</table>";
				$data['content'] = $content;
			}
			else{
				$data['content'] = "ไม่มีการจองในวันนี้";
			}
			
			$this->load->view('reserve_detail', $data);
		}
	}
	//end query reservation detail
	
	//call reserve form function
	public function call_reservation_form()
	{
		if($this->input->post('data'))
		{
			$this->load->view('reserve_calendar_form');
		}
	}
	//end call reserve form function
	
	//check time can or can not reserve function
	public function check_can_cannot_reserve()
	{
		//load model
		$this->load->model('Reservation_model');
		
		//get variable
		$usetime_start = $this->input->post('calendar_time_start');
		$usetime_end = $this->input->post('calendar_time_end');
		$roomname = $this->session->userdata('room');
		$usedate = $this->session->userdata('date');
		
		//check time can or can not reserve
		$can_reserve = $this->Reservation_model->check_time($roomname, $usedate, $usetime_start, $usetime_end);
		
		if($can_reserve == true)
		{
			echo 'C';
		} 
		else 
		{
			echo 'N';
		}
		
	}
	//end check time can or can not reserve function
	
	//reserve from calendar
	public function reserve_calendar()
	{
		//load model
		$this->load->model('Reservation_model');
		
		//get value
		$memid = $this->session->userdata('memid');
		$roomname = $this->session->userdata('room');
		$usedate = $this->session->userdata('date');
		$usetime_start = $this->input->post('calendar_time_start');
		$usetime_end = $this->input->post('calendar_time_end');
		$resdate = date("Y-m-d");
		$restime = date('G:i:s');
		$pcount = $this->input->post('calendar_peoplecount');
		
		//check comcount
		if($this->input->post('calendar_comcount'))
		{
			$comcount = $this->input->post('calendar_comcount');
		}
		
		//get key from random
		$key = $this->generate_reservekey();
		
		//check reservation key
		$checkkey = $this->Reservation_model->check_duplicate_reservekey($key);
		while($checkkey == true)
		{
			$key = $this->generate_reservekey();
			$checkkey = $this->Reservation_model->check_duplicate_reservekey($key);
		}
		
		//check time can or can not reserve
		$can_reserve = $this->Reservation_model->check_time($roomname, $usedate, $usetime_start, $usetime_end);
		
		if($can_reserve == true)
		{
			//for lab room
			if($this->session->userdata('roomtype') == 0)
			{
				$swuse = '';
				$hwuse = '';
				$reserve_into_db = $this->Reservation_model->reserve_lab_room($memid, $roomname, $usedate, $usetime_start, $usetime_end, $resdate, $restime, $swuse, $hwuse, $pcount, $comcount, $key);
			}
			//for meeting room
			if($this->session->userdata('roomtype') == 1)
			{
				$hwuse = '';
				$reserve_into_db = $this->Reservation_model->reserve_meeting_room($memid, $roomname, $usedate, $usetime_start, $usetime_end, $resdate, $restime, $hwuse, $pcount, $key);
			}
			
			//send email
			if($reserve_into_db)
			{
				$this->send_email();
			}
			
			redirect("reservation/status/" .$key);
		}
		else {
			echo "N";
		}
		
		
	}
	//end reserva from calendar
	
	//send email function
	public function send_email()
	{
		//load email model
			$this->load->model('Email_model');
			
			//config message
			$subject = "รายงานผลการจองห้อง";
			$message = "คุณได้ทำการจองห้องเรียบร้อยแล้ว<br/> คุณสามารถตรวจสอบสถานะการจองได้ที่" .base_url() ."reservation/status/" .$key;
			$send_to = $this->session->userdata('email');
			
			//send email
			$this->Email_model->send_email($send_to, $subject, $message);
			redirect("reservation/status/" .$key);
	}
	//end send email function
	
	//generate reserve key 
	public function generate_reservekey()
	{
		$character = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9');
		
		//random key from array
		shuffle($character);
		
		$key = "";
		
		for($i=0; $i<8; $i++)
		{
			$key = $key .$character[$i];
		}
		
		return $key;
		
	}
	//end generate reserve key
	
}